package com.huika.renrench.control.common.environment;

import com.huika.lib.ui.framelayout.KJActivityManager;
import com.huika.renrench.R;
import com.huika.renrench.R.id;
import com.huika.renrench.R.layout;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.os.Build;

public class SetEnvironmentActivity extends BaseAct {

	private RadioGroup rgEnvironment;// 环境选择
	private RadioButton mrbRelease, mrbReleaseTest, mrbTest;// 环境选择
	private TextView tvServerDomain;// 环境具体网络地址显示
	private Button btnOk, btnCancel;// 确认和取消按钮
	private CheckBox chkHideLogoffButton;// 是否屏蔽注销登陆功能
	private boolean mIsReleaseEnv, mIsReleaseTestEnv, mIsHideLogoffButton;

	public static final String IS_RELEASE_ENVIRONMENT = "IS_RELEASE_ENVIRONMENT";
	public static final String IS_RELEASE_TEST_ENVIRONMENT = "IS_RELEASE_TEST_ENVIRONMENT";
	public static final String IS_HIDE_LOGOFF_BUTTON = "IS_HIDE_LOGOFF_BUTTON";

//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		
//	}

	private void initRadioGroup() {
		mIsReleaseEnv = PreferHelper.getInstance().getBoolean(
				IS_RELEASE_ENVIRONMENT, true);
		mIsReleaseTestEnv = PreferHelper.getInstance().getBoolean(
				IS_RELEASE_TEST_ENVIRONMENT, false);
		if (mIsReleaseEnv) {
			mrbRelease.setChecked(true);
		} else if (mIsReleaseTestEnv) {
			mrbReleaseTest.setChecked(true);
		} else {
			mrbTest.setChecked(true);
		}
		rgEnvironment
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.rbRelease:
							tvServerDomain
									.setText(UrlConstants.RELEASE_SERVER_DOMAIN);
							break;
						case R.id.rbReleaseTest:
							tvServerDomain
									.setText(UrlConstants.RELEASE_TEST_SERVER_DOMAIN);
							break;
						case R.id.rbTest:
							tvServerDomain
									.setText(UrlConstants.TEST_SERVER_DOMAIN);
							break;
						}
					}

				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnOk:
			switchVersionEnvironment();
//			System.exit(0);
			KJActivityManager.create().AppExit(getApplicationContext());
			break;
		case R.id.btnCancel:
			finish();
		}
	}

	protected void switchVersionEnvironment() {
		if (mrbRelease.isChecked()) {// 是否选择正式环境
			PreferHelper.getInstance().setBoolean(IS_RELEASE_ENVIRONMENT, true);
		} else {
			if (mrbReleaseTest.isChecked()) {// 是否选择正式环境的测试环境
				PreferHelper.getInstance().setBoolean(IS_RELEASE_ENVIRONMENT,
						false);
				PreferHelper.getInstance()
						.setBoolean(IS_RELEASE_TEST_ENVIRONMENT,
								mrbReleaseTest.isChecked());
			}else{
				PreferHelper.getInstance().setBoolean(IS_RELEASE_ENVIRONMENT,
						false);
				PreferHelper.getInstance()
						.setBoolean(IS_RELEASE_TEST_ENVIRONMENT,
								mrbReleaseTest.isChecked());
			}
		}
//		finish();
		
//		KJActivityManager.create().AppExit(this);
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 一定要退出应用吗？应该可以不退出吧
//		System.exit(0);
	}

	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_set_environment);
		mrbRelease = (RadioButton) findViewById(R.id.rbRelease);
		mrbReleaseTest = (RadioButton) findViewById(R.id.rbReleaseTest);
		mrbTest = (RadioButton) findViewById(R.id.rbTest);
		btnOk = (Button) findViewById(R.id.btnOk);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		rgEnvironment = (RadioGroup) findViewById(R.id.rgEnvironment);
		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		tvServerDomain = (TextView) findViewById(R.id.tvServerDomain);
		chkHideLogoffButton = (CheckBox) findViewById(R.id.chkHideLogoffButton);
		initRadioGroup();
	}
}
